package threadlocal;

import cn.hutool.cron.task.RunnableTask;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangtao
 * @create 2022/3/11 14:03
 */
public class ThreadLocalTest {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,
                1,
                3000,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<>(100),
                new ThreadFactoryBuilder().setNameFormat("test" + "-%d").build(),
                new ThreadPoolExecutor.AbortPolicy() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {

                    }
                });
        ExecutorService executorService = TtlExecutors.getTtlExecutorService(executor);

        TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();


        context.set("value-set-in-parent");
        executorService.submit(() -> {
            String value = context.get();
            System.out.println("executorService " + value);
        });

        context.set("value-set-in-parent1");
        executor.submit(() -> {
            String value = context.get();
            System.out.println("executor " + value);
        });

        executorService.submit(() -> {
            String value = context.get();
            System.out.println("executorService " + value);
        });
    }
}
